package com.springboot.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.demo.annotation.MoreSerializeField;
import com.springboot.demo.annotation.MultiSerializeField;
import com.springboot.demo.annotation.SerializeField;
import com.springboot.demo.bean.Address;
import com.springboot.demo.bean.User;
import com.springboot.demo.service.UserService;

@RestController
@RequestMapping(value = "/api/v1")
public class MainController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String get() {
        return "hello";
    }

    // 输出一个pojo
    @RequestMapping(value = "/1", method = RequestMethod.GET)
    // use annotation to customize your json output *_*
    // clazz -- the POJO class that you want to customize
    // includes -- the properties in POJO that you want to save
    @SerializeField(clazz = User.class, includes = { "password" })
    public User getUser() {

        return new User("234", "456");
    }

    // 输出两个个pojo
    @RequestMapping(value = "/2", method = RequestMethod.GET)
    // use annotation to customize your json output *_*
    // clazz -- the POJO class that you want to customize
    // includes -- the properties in POJO that you wan to save
    @SerializeField(clazz = Address.class, includes = { "school", "home", "user" })
    // use @MultiSerializeField annotation to customize the second POJO json
    // output *_*
    // clazz -- the POJO class that you want to customize
    // excludes -- the properties in POJO that you do not want to show in json
    // output
    @MultiSerializeField(clazz = User.class, excludes = { "addresses" })
    public Address getAddressById(@PathVariable Long id) {

        return new Address();
    }

    // 输出多个个pojo
    @RequestMapping(value = "/3", method = RequestMethod.POST)
    // use @MoreSerializeField annotation to customize mutiple POJO json output
    // *_*
    // add @SerializeField annotation as many as you want :)
    @MoreSerializeField({
            @SerializeField(clazz = Address.class, includes = { "school", "home", "user" }),
            @SerializeField(clazz = User.class, includes = { "id", "name" }) })
    public Address createAddress(Address address) {
        return new Address();
    }

}
